﻿using System;
using System.Globalization;
using System.Windows.Forms;
using System.Text;
using Irclib;

namespace DeWildIRC
{
    public partial class MainWindow : Form
    {
        Irclib.IrcConnection _connection;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void mainEntryTextBox_TextChanged(object sender, EventArgs e)
        {
            if (mainEntryTextBox.Text.EndsWith("\r\n", true, CultureInfo.InvariantCulture))
            {
                SendMessage(mainEntryTextBox.Text);
                mainEntryTextBox.Text = string.Empty;
            }
           
        }

        private void SendMessage(string toSend)
        {
            if (toSend.StartsWith("/sslserver", true, CultureInfo.InvariantCulture))
            {
                string[] tokens = toSend.Split(' ');
                Uri serverUri = new Uri(tokens[1].Remove(tokens[1].Length - 2));
                _connection = new SecureIrcConnection(serverUri, Encoding.ASCII, true, "foobar");
                _connection.Connect();
                _connection.Initialize();
                
            }

            if (toSend.StartsWith("/quit", true, CultureInfo.InvariantCulture))
                if (this._connection != null)
                {
                    this._connection.Dispose();
                    this._connection = null;
                }

        }
    }
}
